package cn.jyc.ssm03.service;

import cn.jyc.ssm03.dao.CarMapper;
import cn.jyc.ssm03.dao.ChecksMapper;
import cn.jyc.ssm03.dao.RentsMapper;
import cn.jyc.ssm03.pojo.Bus_checks;
import cn.jyc.ssm03.pojo.Bus_rents;
import cn.jyc.ssm03.pojo.Car;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChkService {
    @Autowired
    private ChecksMapper cm;
    @Autowired
    private CarMapper carm;
    @Autowired
    private RentsMapper rentm;

    public Map<String, String> addChk(Bus_checks bc) {
        Map<String, String> res = new HashMap<>();
        try {
            List<Bus_rents> bus_rents = rentm.qryRents(new Bus_rents(bc.getRentid()));
            if (bus_rents.get(0).getRentflag()==1){
                throw new RuntimeException("该车辆不是出租状态！");
            }
            cm.addChk(bc);
            String carnumber = bus_rents.get(0).getCarnumber();
            carm.updCar(new Car(carnumber,null,null,null,null,null,0,null,null));
            rentm.updRent(new Bus_rents(bc.getRentid(),1));
        } catch (Exception e){
            res.put("errorMsg",e.getMessage());
            return res;
        }
        res.put("ok","添加成功！");
        return res;
    }

    public Map<String, Object> qryChks(Integer p1, Integer p2, Bus_checks bc) {
//        System.out.println(p1+"---"+p2+"---"+br);
        Map<String,Object> res = new HashMap<>();
        Page<Object> pg = PageHelper.startPage(p1, p2);
        List<Bus_checks> brs = cm.qryChks(bc);
//        System.out.println(brs.size());
//        System.out.println(brs.get(0));
        res.put("total",pg.getTotal());
        res.put("rows",brs);
        return res;
    }
}
